<html>
<body>
<form method="GET">
   <input type="text" name="calc">
   <input type="submit" value="Calculate">
</form>

<?php

   //Prepare regex
   $term = "(\s*-?\d+\.?\d*\s*|\s*-?\d*\.?\d+\s*)";
   $operator = "[\-+*/]";
   $valid = "#^$term($operator$term)*$#";
   
   $expr = $_GET["calc"];
   if ( $expr != '' )
   {
      if ( preg_match($valid, $expr) && !preg_match("#/\s*-?0#", $expr) )
      {
         $expr = preg_replace("/(--)|(\+\+)/", '+', $expr);
         eval("\$value = $expr;");
         echo $value;
      }
      else
      {
         echo "Invalid input";
      }
   }

?>

</body>
</html>
